<template>
	<view>
		<uni-popup ref="popup" :mask-click="false">
			<view class="example">
				<scroll-view class="scroll-Y" scroll-y="true">
					<uni-forms ref="dynamicForm" :rules="dynamicRules" :modelValue="reportQuality">
						<!-- 质检单主表信息 -->
						<uni-forms-item label="质检时间" labelWidth="80" required>
							<uni-datetime-picker type="datetime" return-type="timestamp"
								v-model="reportQuality.checkTime" :disabled="disabledFlag" :clear-icon="false" />
						</uni-forms-item>
						<uni-forms-item label="质检员 ">
							<uni-easyinput v-model="reportQuality.checkUserName" :disabled="true" />
						</uni-forms-item>
						<uni-forms-item label="扫码SN码" :labelWidth='80' style="margin-bottom: 10rpx; ">
							<uni-easyinput v-model="reportQuality.snArray" type="textarea"
								placeholder="请直接使用PDA扫描物料上的SN码"></uni-easyinput>
						</uni-forms-item>
						<view style="background-color: #d1f2d7;">
							<uni-row>
								<uni-col :span="20">
									<uni-forms-item label="合格数  " required labelWidth="80"
										style="margin-bottom: 10rpx;">
										<uni-easyinput v-model="reportQuality.qualifiedQuantity" placeholder="请输入合格数量"
											type="digit" :disabled="disabledFlag" :clearable="false" />
									</uni-forms-item>
								</uni-col>
								<uni-col :span="40" style="margin-top: 6rpx;">
									<uni-icons type="plus" size="30" style="margin-left: 24rpx;"
										@click="addReportQualityEntry('qualified')"
										v-show="disabledFlag==true?false:true">
									</uni-icons>
								</uni-col>

							</uni-row>

							<view v-for="(item,index) in tmpQualityLevelList">
								<uni-row style="margin-bottom: 6rpx;">
									<uni-col :span="10">
										<uni-data-select v-model="item.levelId" :localdata="qualityLevelList"
											placeholder="合格等级" :disabled="disabledFlag" :clear="false">
										</uni-data-select>
									</uni-col>
									<uni-col :span="8">
										<uni-easyinput v-model="item.quantity" placeholder="请输入数量" type="digit"
											style="margin-left: 8rpx;" :disabled="disabledFlag" :clearable="false" />
									</uni-col>
									<uni-col :span="3">
										<uni-icons type="plus" size="30" style="margin-left: 24rpx;"
											@click="clickReason('qualified',index)"
											v-show="disabledFlag==true?false:true">
										</uni-icons>
									</uni-col>
									<uni-col :span="3">
										<uni-icons type="minus" size="30" style="margin-left: 12rpx;"
											@click="deleteQualityEntry('qualified',index)"
											v-show="disabledFlag==true?false:true"></uni-icons>
									</uni-col>
								</uni-row>
								<view v-for="(reasonItem,reasonIndex) in item.reasonList">
									<uni-row style="margin-left: 50rpx;margin-bottom: 3rpx;">
										<uni-col :span="10">
											<uni-data-select v-model="reasonItem.reasonId" :localdata="reasonList"
												placeholder="合格原因" :disabled="disabledFlag" :clear="false">
											</uni-data-select>
										</uni-col>
										<uni-col :span="10">
											<uni-easyinput v-model="reasonItem.quantity" placeholder="请输入数量"
												type="digit" style="margin-left: 4rpx;" :disabled="disabledFlag"
												:clearable="false" />
										</uni-col>
										<uni-col :span="4">
											<uni-icons type="minus" size="30" style="margin-left: 24rpx;"
												@click="deleteReason('qualified',index,reasonIndex)"
												v-show="disabledFlag==true?false:true"></uni-icons>
										</uni-col>
									</uni-row>
								</view>
							</view>
						</view>
						<view style="background-color: #fad8d6;">
							<uni-row style="margin-top: 40rpx;">
								<uni-col :span="20">
									<uni-forms-item label="不合格数" required labelWidth="80" style="margin-bottom: 10rpx;">
										<uni-easyinput v-model="reportQuality.unqualifiedQuantity"
											placeholder="请输入不合格数量" type="digit" style="margin-left: 8rpx;"
											:disabled="disabledFlag" :clearable="false" />
									</uni-forms-item>
								</uni-col>
								<uni-col :span="40" style="margin-top: 6rpx;">
									<uni-icons type="plus" size="30" style="margin-left: 24rpx;"
										@click="addReportQualityEntry('unQualified')"
										v-show="disabledFlag==true?false:true"></uni-icons>
								</uni-col>
							</uni-row>
							<view v-for="(item,index) in tmpUnQualityLevelList" :key="index">
								<uni-row>
									<uni-col :span="10">
										<uni-data-select v-model="item.levelId" :localdata="unQualityLevelList"
											placeholder="不合格等级" :disabled="disabledFlag"
											:clear="false"></uni-data-select>
									</uni-col>
									<uni-col :span="8">
										<uni-easyinput v-model="item.quantity" placeholder="请输入数量" type="digit"
											:disabled="disabledFlag" :clearable="false" />
									</uni-col>
									<uni-col :span="3">
										<uni-icons type="plus" size="30" style="margin-left: 24rpx;"
											@click="clickReason('unQualified',index)"
											v-show="disabledFlag==true?false:true"></uni-icons>
									</uni-col>
									<uni-col :span="3">
										<uni-icons type="minus" size="30" style="margin-left: 12rpx;"
											@click="deleteQualityEntry('unQualified',index)"
											v-show="disabledFlag==true?false:true"></uni-icons>
									</uni-col>
								</uni-row>
								<view v-for="(reasonItem,reasonIndex) in item.reasonList">
									<uni-row style="margin-left: 50rpx;margin-bottom: 3rpx;">
										<uni-col :span="10">
											<uni-data-select v-model="reasonItem.reasonId" :localdata="unReasonList"
												placeholder="不合格原因" :disabled="disabledFlag" :clear="false">
											</uni-data-select>
										</uni-col>
										<uni-col :span="10">
											<uni-easyinput v-model="reasonItem.quantity" placeholder="请输入数量"
												type="digit" style="margin-left: 4rpx;" :disabled="disabledFlag"
												:clearable="false" />
										</uni-col>
										<uni-col :span="4">
											<uni-icons type="minus" size="30" style="margin-left: 24rpx;"
												@click="deleteReason('unQualified',index,reasonIndex)"
												v-show="disabledFlag==true?false:true"></uni-icons>
										</uni-col>
									</uni-row>
								</view>
							</view>
						</view>
					</uni-forms>
					<view class="button-group">
						<button type="primary" size="mini" @click="close()">取消</button>
						<button type="primary" size="mini" @click="submit()"
							v-show="disabledFlag==true?false:true">确认</button>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
		<pdaScan></pdaScan>
	</view>
</template>

<script>
	import {
		getStation
	} from "@/api/masterdata/station";
	import {
		listForMobile,
		getInfoByEntryId,
		getWorkOrder
	} from "@/api/production/workOrder";
	import {
		mobileBindUserStation,
		getInfoByUserId
	} from "@/api/common/sysUserStation";
	import storage from '@/utils/storage'
	import {
		scanCodeBindStation
	} from '@/utils/mesUntil.js'
	import {
		listReport,
		getReport,
		delReport,
		addReport,
		updateReport,
		batchSave
	} from "@/api/production/report";
	import {
		getUserProfile
	} from "@/api/system/user"
	import {
		listQualityLevel
	} from "@/api/quality/qualityLevel";
	import {
		listReason
	} from "@/api/quality/reason";
	import {
		addReportQuality,
		listReportQuality,
		getReportQuality
	} from "@/api/quality/reportQuality";
	import pdaScan from "@/components/mes/pdaScan.vue"
	export default {
		name: "reportQualityForm",
		components: {
			pdaScan
		},
		data() {
			return {
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					orderByColumn: '',
					isAsc: "",
					/* 移动端用的工单状态,与字典中稍有差异 */
					mobileStatus: "B",
				},
				//报工质检单
				reportQuality: {},
				user: {},
				dynamicRules: {

				},
				//质检合格等级列表
				qualityLevelList: [],
				//质检不合格等级列表
				unQualityLevelList: [],
				//质检原因合格列表
				reasonList: [],
				//质检原因不合格列表
				unReasonList: [],
				tmpQualityLevelList: [],
				tmpUnQualityLevelList: [],
				disabledFlag: false,
			}
		},
		created() {
			var _this = this
			uni.$off('scancodedate') // 每次进来先 移除全局自定义事件监听器  
			uni.$on('scancodedate', function(data) {
				_this.reportQuality.snArray += "'" + data.code + "',";
				let arr = _this.reportQuality.snArray.split(",")
				if (arr.length % 2 != 0) {
					_this.reportQuality.snArray += "\r\n"
				}
				console.log('你想要的code：', _this.reportQuality)
			})
		},
		methods: {

			getUser() {
				getUserProfile().then(response => {
					this.user = response.data
				})
			},

			submit() {
				this.$refs.dynamicForm.validate().then(res => {
					this.reportQuality.qualityLevelList = [];
					for (let i = 0; i < this.tmpQualityLevelList.length; i++) {
						//给等级名称赋值
						let opt = {};
						opt = this.qualityLevelList.find((item) => {
							return item.id === this.tmpQualityLevelList[i].levelId;
						});
						this.tmpQualityLevelList[i].levelName = opt.name;
						//给原因名称赋值
						let reasonList = this.tmpQualityLevelList[i].reasonList;
						for (let j = 0; j < reasonList.length; j++) {
							let optB = {};
							optB = this.reasonList.find((item) => {
								return item.id === reasonList[j].reasonId;
							});
							reasonList[j].reasonName = optB.name;
						}
						this.reportQuality.qualityLevelList.push(this.tmpQualityLevelList[i])
					}
					for (let i = 0; i < this.tmpUnQualityLevelList.length; i++) {
						//给等级名称赋值
						let opt = {};
						opt = this.unQualityLevelList.find((item) => {
							return item.id === this.tmpUnQualityLevelList[i].levelId;
						});
						this.tmpUnQualityLevelList[i].levelName = opt.name;
						//给原因名称赋值
						let reasonList = this.tmpUnQualityLevelList[i].reasonList;
						for (let j = 0; j < reasonList.length; j++) {
							let optB = {};
							optB = this.unReasonList.find((item) => {
								return item.id === reasonList[j].reasonId;
							});
							reasonList[j].reasonName = optB.name;
						}

						this.reportQuality.qualityLevelList.push(this.tmpUnQualityLevelList[i])
					}
					addReportQuality(this.reportQuality).then(response => {
						this.$modal.msgSuccess("新增成功");
						this.close();
					});

				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},

			//通过报工单打开质检单
			openPopup(row) {
				this.disabledFlag = false;
				//获取下拉框数据
				listQualityLevel({
					pageSize: 99999,
					status: '0'
				}).then(response => {
					let list = [];
					list = response.rows;
					//区分合格和不合格等级
					for (let i = 0; i < list.length; i++) {
						if ("A" === list[i].type) {
							list[i].value = list[i].id;
							list[i].text = list[i].name
							this.qualityLevelList.push(list[i]);
						}
						if ("B" === list[i].type) {
							list[i].value = list[i].id;
							list[i].text = list[i].name
							this.unQualityLevelList.push(list[i]);
						}
					}
				});
				listReason({
					pageSize: 99999,
					status: '0'
				}).then(response => {
					let list = [];
					list = response.rows;
					//区分合格和不合格等级
					for (let i = 0; i < list.length; i++) {
						if ("A" === list[i].type) {
							list[i].value = list[i].id;
							list[i].text = list[i].name;
							this.reasonList.push(list[i]);
						}
						if ("B" === list[i].type) {
							list[i].value = list[i].id;
							list[i].text = list[i].name;
							this.unReasonList.push(list[i]);
						}
					}
				});
				//通过报工单ID查其质检单
				listReportQuality({
					pageSize: 99999,
					reportId: row.id
				}).then(qualityResp => {

					this.tmpQualityLevelList = [];
					this.tmpUnQualityLevelList = [];
					if (qualityResp.rows.length > 0) {
						this.disabledFlag = true;
						let list = qualityResp.rows[0].qualityLevelList;
						//有质检单赋值
						this.reportQuality = qualityResp.rows[0];

						for (let i = 0; i < list.length; i++) {
							let things = [];
							things = list[i].reasonList;
							console.log("things===>", things)
							for (var j = 0; j < things.length; j++) {}
							if (list[i].type === "A") {
								this.tmpQualityLevelList.push(list[i]);
							}
							if (list[i].type === "B") {
								this.tmpUnQualityLevelList.push(list[i]);
							}
						}
					} else {
						getUserProfile().then(userResp => {
							this.user = userResp.data
							//没有质检单进行初始化
							this.reportQuality = {
								reportId: row.id,
								reportNumber: row.number,
								workOrderNumber: row.workOrderNumber,
								processName: row.processName,
								materialNumber: row.materialNumber,
								materialName: row.materialName,
								specification: row.specification,
								materialUnitName: row.materialUnitName,
								batchNumber: row.batchNumber,
								qcType: row.qcType,
								quantity: 1,
								qualifiedQuantity: 1,
								unqualifiedQuantity: 0,
								reportTime: row.reportTime,
								checkTime: new Date().format("yyyy-MM-dd HH:mm:ss"),
								checkUserId: this.user.userId,
								checkUserName: this.user.nickName,
								status: '0',
								snArray: '',
								qualityLevelList: []
							};
						})
					}
					this.open = true;
				})
				this.$refs.popup.open();
			},
			close() {
				this.reportQuality = {};
				this.qualityLevelList = [];
				this.unQualityLevelList = [];
				this.reasonList = [];
				this.unReasonList = [];
				this.tmpQualityLevelList = [];
				this.tmpUnQualityLevelList = [];
				this.$refs.popup.close();
			},
			addReportQualityEntry(type) {

				//合格
				if (type === "qualified") {
					this.tmpQualityLevelList.push({
						quantity: 1,
						type: 'A',
						reasonList: []
					})
				} else {
					//不合格
					this.tmpUnQualityLevelList.push({
						quantity: 1,
						type: 'B',
						reasonList: []
					})
				}
				this.$forceUpdate();
			},
			clickReason(type, index) {
				//合格
				if (type === "qualified") {
					this.tmpQualityLevelList[index].reasonList.push({
						reasonId: 5,
						quantity: 1
					})

				} else {
					//不合格
					this.tmpUnQualityLevelList[index].reasonList.push({
						reasonId: 2,
						quantity: 1
					})
				}
				this.$forceUpdate();
			},
			deleteQualityEntry(type, index) {

				if (type === "qualified") {
					this.tmpQualityLevelList.splice(index, 1);
				} else {
					this.tmpUnQualityLevelList.splice(index, 1);
				}
				console.log("this.tmpQualityLevelList===>", this.tmpQualityLevelList)
			},
			deleteReason(type, index, reasonIndex) {
				if (type === "qualified") {
					this.tmpQualityLevelList[index].reasonList.splice(reasonIndex, 1);
				} else {
					this.tmpUnQualityLevelList[index].reasonList.splice(reasonIndex, 1);
				}
			},
			//详情
			qualityInfo(id) {
				this.disabledFlag = true;
				//获取下拉框数据
				listQualityLevel({
					pageSize: 99999,
					status: '0'
				}).then(response => {
					let list = [];
					list = response.rows;
					//区分合格和不合格等级
					for (let i = 0; i < list.length; i++) {
						if ("A" === list[i].type) {
							this.qualityLevelList.push(list[i]);
						}
						if ("B" === list[i].type) {
							this.unQualityLevelList.push(list[i]);
						}
					}
				});
				listReason({
					pageSize: 99999,
					status: '0'
				}).then(response => {
					let list = [];
					list = response.rows;
					//区分合格和不合格等级
					for (let i = 0; i < list.length; i++) {
						if ("A" === list[i].type) {
							this.reasonList.push(list[i]);
						}
						if ("B" === list[i].type) {
							this.unReasonList.push(list[i]);
						}
					}
				});
				getReportQuality(id).then(quantityRes => {
					this.reportQuality = quantityRes.data;
					this.tmpQualityLevelList = [];
					this.tmpUnQualityLevelList = [];
					let list = this.reportQuality.qualityLevelList;
					for (let i = 0; i < list.length; i++) {
						if (list[i].type === "A") {
							this.tmpQualityLevelList.push(list[i]);
						}
						if (list[i].type === "B") {
							this.tmpUnQualityLevelList.push(list[i]);
						}
					}
					this.$refs.popup.open();
				})

			},
		},

	}
</script>

<style>
	.example {
		padding: 16rpx;
		margin: 0rpx;
		background-color: #fff;
		width: 720rpx;
	}



	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}

	.bg-purple-light {
		border-radius: 4px;
		min-height: 36px;
		background: #FFFFFF;
	}

	.scroll-Y {
		height: 900rpx;
		width: 96%;
	}
</style>